import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/book/BookItem.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/readingLeveledDataGrid.dart';
import '../../themes/appTheme.dart';

class LeveledReadingRecords extends StatefulWidget {
  static const String routeName = '/student/report/leveledreadings';

  @override
  State<LeveledReadingRecords> createState() => _LeveledReadingRecordsState();
}

class _LeveledReadingRecordsState extends State<LeveledReadingRecords> {
  final box = Hive.box('appconfig');
  final BooksController _booksController = Get.find();
  bool isStudentOrIndividual = false;
  List<BookItem> bookItems = [];

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    isStudentOrIndividual = box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Individual ||
        box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Student;

    if (isStudentOrIndividual) {
      _booksController.fetchProgressReadingHistory(120, 0).then((result) {
        setState(() {
          bookItems = result;
        });
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student/individual leveled reading records');

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.0),
          color: Get.isDarkMode
              ? AppTheme.colors.darkTheme2
              : Colors.black.withOpacity(0.1),
        ),
        child: Row(
          children: [
            Expanded(
              flex: 10,
              child: Container(
                margin: EdgeInsets.all(12.0),
                padding: EdgeInsets.all(
                  30,
                ),
                decoration: BoxDecoration(
                    color: Get.isDarkMode
                        ? AppTheme.colors.darkTheme2
                        : Colors.white.withOpacity(0.9),
                    borderRadius: BorderRadius.circular(3.0)),
                child: LeveledReadingHistoryDataGrids(bookItems),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
